﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiaoYuStock.Model.Request;
using XiaoYuStock.Model;
using XiaoYuStock.Model.Models;
using XiaoYuStockAnalysis.BusinessLogic.Business;
using XiaoYuStock.Core;
using System.IO;
using XiaoYuStock.AppService.State;
using XiaoYuStock.AppService.Storage;
using XiaoYuStock.Model.Entities;
namespace XiaoYuStock.AppService
{
   public class AnalyService
    {

        private ShareListDataBusiness shareBus = new ShareListDataBusiness();

        private HistoryDataBusiness historyBus = new HistoryDataBusiness();

       /// <summary>
       /// 时长
       /// </summary>
        public int Days { get; set; }

        public AnalyService() { this.Days = 3; }
        public AnalyService(int days) { this.Days = days; }

        public static List<AnalysedState> NeedAnalysedStates { get; set; }
        static AnalyService()
        {
            NeedAnalysedStates = new List<AnalysedState>();
            var types = Enum.GetValues(typeof(AnalysedType));
            foreach (var type in types)
            {
                var t = (AnalysedType)type;
                NeedAnalysedStates.Add(AnalysedStateFactory.GetInstance(t));

            }
        }


       /// <summary>
       /// 分析数据
       /// </summary>
       /// <param name="days"></param>
       /// <param name="guidePrice"></param>
        public void AnalyData(int days)
        {
            this.Days = days;
            foreach (var m in NeedAnalysedStates)
            {
                m.Days = days;
                m.Storage = StorageFactory.GetInstance();
            }

            int totalRecord = 0;
            ShareRequest shareRequest = new ShareRequest();
            shareRequest.PageSize = 100;
            shareRequest.ShareStatus = (int)ShareState.Active;
            shareRequest.PageIndex = 1;
            int pageCount = 0; 
            do
            {
               var  result = shareBus.GetList(shareRequest,out totalRecord);
                if (pageCount == 0)
                {
                    pageCount = (totalRecord + shareRequest.PageSize - 1) / shareRequest.PageSize;
                }
                foreach (var item in result)
                {
                    LogHelper.InfoAsync("执行统计分析：" + item.ShareCode);
                    var history = GetHistoryData(item.ShareCode);
                    foreach (var m in NeedAnalysedStates)
                    {
                       // m.AnalyShareResultSave(item.ShareCode, history);
                    }
                }
                shareRequest.PageIndex++;
               
            }
            while (shareRequest.PageIndex<= pageCount);

        

        }

       




       /// <summary>
       /// 获取历史数据
       /// </summary>
       /// <param name="shareCode"></param>
       /// <param name="days"></param>
       /// <returns></returns>
        private IEnumerable<HistoryData> GetHistoryData(string shareCode)
        {
            HistoryRequest historyRequest = new HistoryRequest();
            historyRequest.ShareCode = shareCode;
            historyRequest.PageSize = this.Days;
            return historyBus.GetDataList(historyRequest);
        }

       /// <summary>
       /// 获取全部股票进行分析
       /// </summary>
       /// <param name="action"></param>
        private List<string> GetStatisticsData(Func<IEnumerable<HistoryData>, bool> func)
        {
            List<string> resultData = new List<string>();
            int totalRecord = 0;
            ShareRequest shareRequest = new ShareRequest();
            shareRequest.PageSize = 100;
            shareRequest.ShareStatus = (int)ShareState.Active;
            shareRequest.PageIndex = 1;
            int pageCount = 0; 
            do
            {
                var result = shareBus.GetList(shareRequest,out totalRecord);
                if (pageCount == 0)
                {
                   pageCount = (totalRecord + shareRequest.PageSize - 1) / shareRequest.PageSize;
                }
                foreach (var item in result)
                {
                    LogHelper.InfoAsync("执行统计分析：" + item.ShareCode);
                    if (func != null)
                    {
                        var history = GetHistoryData(item.ShareCode);
                        if (func(history))
                            resultData.Add(item.ShareCode);
                    }
                }
                shareRequest.PageIndex++;
                
            }
            while (shareRequest.PageIndex <= pageCount);
            return resultData;
        }

       
     
    }
}
